package com.tianma315.admin.qa.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.tianma315.admin.common.base.CoreServiceImpl;
import com.tianma315.admin.qa.domain.QaSpotCheckDO;
import com.tianma315.admin.qa.vo.QaCheckVO;
import com.tianma315.admin.qa.vo.QaSpotCheckVO;
import com.tianma315.admin.sys.domain.UserDO;
import com.tianma315.admin.sys.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.tianma315.admin.qa.dao.QaCheckDao;
import com.tianma315.admin.qa.domain.QaCheckDO;
import com.tianma315.admin.qa.service.QaCheckService;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * 
 * <pre>
 * 品控检查
 * </pre>
 * <small> 2019-07-09 16:02:56 | wen</small>
 */
@Service
public class QaCheckServiceImpl extends CoreServiceImpl<QaCheckDao, QaCheckDO> implements QaCheckService {

    @Autowired
    private QaCheckDao qaCheckDao;

    @Autowired
    private UserService userService;

    @Override
    public Page<QaCheckVO> getQaCheckPage(Integer pageNumber, Integer pageSize, QaCheckVO qaCheckDTO , Date beginDate,
                                          Date endDate) {
        // 查询列表数据
        Page<QaCheckVO> page = new Page<>(pageNumber, pageSize);

//        Wrapper<QaCheckDO> wrapper = new EntityWrapper<QaCheckDO>(qaCheckDTO);
//        wrapper.eq("state",0);
//        page = selectPage(page, wrapper);
//        int total = selectCount(wrapper);
//        page.setTotal(total);
        List<QaCheckVO> qaCheckVOList = qaCheckDao.getQaCheckVOPage(page,qaCheckDTO,beginDate,endDate);
        page.setRecords(qaCheckVOList);
        return page;
    }

    @Override
    public Boolean addQaCheckDO(QaCheckDO qaCheckDO) {
        qaCheckDO.setState(0);
        qaCheckDO.setCreateDate(Calendar.getInstance().getTime());
        Boolean result = insert(qaCheckDO);
        return result;
    }

    @Override
    public Boolean deleteQaCheckDO(Integer id) {
        QaCheckDO qaCheckDO = new QaCheckDO();
        qaCheckDO.setState(1);
        qaCheckDO.setQaCheckId(id);
        Boolean result = updateById(qaCheckDO);
        return result;
    }

    @Override
    public QaCheckVO getQaCheckByProductIdAndBatch(Long productId, String batch) {
        Wrapper<QaCheckDO> wrapper = new EntityWrapper<>();
        wrapper.eq("product_id",productId);
        wrapper.eq("batch",batch);
        wrapper.eq("state",0);
        QaCheckDO qaCheckDO = selectOne(wrapper);
        QaCheckVO qaCheckVO = new QaCheckVO();
        if (qaCheckDO != null) {
            UserDO user = userService.selectById(qaCheckDO.getCheckBy());
            BeanUtils.copyProperties(qaCheckDO,qaCheckVO);
            qaCheckVO.setCheckName(user.getName());
        }
        return qaCheckVO;
    }

    @Override
    public List<QaCheckVO> getQaCheckByBatch(String batch) {
        List<QaCheckVO> qaCheckByBatch = qaCheckDao.getQaCheckByBatch(batch);
        return qaCheckByBatch;
    }
}
